package org.dxy.trigger.http;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dxy.api.dto.request.material.*;
import org.dxy.api.response.R;
import org.dxy.infrastructure.persistent.db.mybatispuls.po.query.MaterialViewPo;
import org.dxy.trigger.http.service.MaterialApplicationService;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 物料管理控制器
 * @author dxy
 * @createTime 2025/9/7
 */
@Slf4j
@RestController
@RequestMapping("/api/materials")
@RequiredArgsConstructor
public class MaterialController {

    private final MaterialApplicationService materialApplicationService;

    /**
     * 创建物料
     */
    @PostMapping("/create")
    public R<Long> createMaterial(@Validated @RequestBody CreateMaterialRequest request) {
        return materialApplicationService.create(request);
    }

    /**
     * 分页查询物料列表
     */
    @GetMapping("/list")
    public R<Page<MaterialViewPo>> list(
            @ParameterObject Page<MaterialViewPo> page,
            @ParameterObject MaterialViewPo query) {
        return materialApplicationService.list(page, query);
    }

    /**
     * 更新物料信息
     */
    @PutMapping("/{id}")
    public R<Void> update(@PathVariable Long id, @Validated @RequestBody UpdateMaterialRequest request) {
        return materialApplicationService.update(id, request);
    }


    /**
     * 启用物料
     */
    @PutMapping("/{id}/enable")
    public R<Void> enable(@PathVariable Long id) {
        return materialApplicationService.enable(id);
    }

    /**
     * 禁用物料
     */
    @PutMapping("/{id}/disable")
    public R<Void> disable(@PathVariable Long id) {
        return materialApplicationService.disable(id);
    }

    /**
     * 删除物料
     */
    @DeleteMapping("/{id}")
    public R<Void> delete(@PathVariable Long id) {
        return materialApplicationService.delete(id);
    }

    /**
     * 批量删除物料
     */
    @DeleteMapping("/batch")
    public R<Void> batchDelete(@RequestBody List<Long> ids) {
        return materialApplicationService.batchDelete(ids);
    }

    /**
     * 获取物料详情
     */
    @GetMapping("/{id}")
    public R<MaterialViewPo> getById(@PathVariable Long id) {
        return materialApplicationService.getById(id);
    }

    /**
     * 根据群组获取物料列表
     */
    @GetMapping("/group/{groupId}")
    public R<List<MaterialViewPo>> getByGroupId(@PathVariable Long groupId) {
        return materialApplicationService.getByGroupId(groupId);
    }

    /**
     * 获取启用状态的物料列表
     */
    @GetMapping("/active")
    public R<List<MaterialViewPo>> getActiveList() {
        return materialApplicationService.getActiveList();
    }
}